﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using System.Web;
using System.Configuration;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// ERP客户档案_同步低代码
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ErpInfoSendDDMController : ControllerBase
    {
        private readonly ILogger<CustomerToDDMRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_erp;

        public ErpInfoSendDDMController(ILogger<CustomerToDDMRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_erp = _configuration.GetConnectionString("ERP_DbConnection");
        }

        /// <summary>
        /// ERP客户档案_同步低代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostCustomerToDDM([FromBody] CustomerToDDMRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WaybillInfoSendWMSResponseDto re = new WaybillInfoSendWMSResponseDto();
            WaybillInfoSendWMSRequestDto pus = new WaybillInfoSendWMSRequestDto();
            
            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
                strSql = new StringBuilder();
                strSql.Append(@"select distinct CUSTOMER_NUMBER,CUSTOMER_NAME,VAT_REGISTRATION_NUM,
CASE WHEN  REGEXP_LIKE(CUSTOMER_NAME, '[A-Za-z]')  THEN '国外'  
        ELSE '国内'  
    END   CUSTOMER_TYPE
  from  apps.CUX_OA_CUSTOMERS_V  WHERE 1=1      
     ");
                DataTable push_dt = DB.OracleQuery(strSql.ToString(), con_erp);//oracle数据库外部查询
                if (push_dt.Rows.Count > 0)
                {
                    foreach (DataRow item in push_dt.Rows)
                    {
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("customer_code", item["CUSTOMER_NUMBER"].ToString());//查询参数
                        DataTable check_kh_dt = MethodHelp.GetTableInfo("TMS0047", dic, con);//卡控已经生成的客户
                        if (check_kh_dt.Rows.Count<=0)
                        {
                            dic = new Dictionary<string, string>();                          
                            dic.Add("customer_code", item["CUSTOMER_NUMBER"].ToString());
                            dic.Add("customer_name", item["CUSTOMER_NAME"].ToString());
                            dic.Add("tax_code", item["VAT_REGISTRATION_NUM"].ToString());
                            dic.Add("customer_type", item["CUSTOMER_TYPE"].ToString());
                            string strSQL = MethodHelp.GetTranSQL("TMS0046", dic, con);
                            if (strSQL.Trim() != "")
                            {
                                strSqlList.Add(strSQL.ToString());
                            }
                            else
                            {
                                strMsg = "TMS0046 Parameter conversion failed";
                            }
                        }                      
                    }
                    if (strMsg == "")
                    {
                        if (strSqlList.Count > 0)
                        {
                            bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                            if (A)
                            {
                              

                            }


                        }
                    }
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.code = 0;
               
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
